const app = getApp()
import { request } from '../../utils/request'
import config from '../../utils/config'
Page({
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        leftGood:[],
        rightGood:[],
        page:1,
        last_page:1,
        good_type:'',
        is_new:'',
        endHide:true,
        loading:true,
        visible1:false,
        brand_id:'',
        brand:{},
        user:{},
        home:false
    },
    onLoad(options) {
        wx.showShareMenu({
            menus: ["shareAppMessage","shareTimeline"],
          })
        var user = wx.getStorageSync('userInfo')
        this.setData({brand_id:options.brand_id,user})
        this.getBrandDetail()
        this.getGoodList()
        this.setBrandViewAdd()
        var op = wx.getLaunchOptionsSync()
        var pages = getCurrentPages(); // 获取页面栈
        var prevPage = pages[pages.length - 2]; // 上一个页面
        if(!prevPage && op.scene >= 1007){
            this.setData({home:true})
        }else{
            this.setData({home:false})
        }
    },
    onReachBottom() {
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }
        this.setData({page:this.data.page+1})
        this.getGoodList()
    },
    getGoodList(){
        request({
            url:"/miniprogram/good/getBrandGoodList",
            data:{brand_id:this.data.brand_id,page:this.data.page},
            type:'post'
        }).then(res=>{
            this.setData({
                last_page:res.last_page
            })
            if(res.data.length == 0){
                return false
            }
            var left = [],
                right = []
            for(let i in res.data){
                if(i%2){
                    right.push(res.data[i])
                }else{
                    left.push(res.data[i])
                }
            }
            if(this.data.page == 1){
                this.setData({leftGood:[],rightGood:[]})
            }
            this.setData({
                leftGood:this.data.leftGood.concat(left),
                rightGood:this.data.rightGood.concat(right)
            })
        })
    },
    getBrandDetail(){
        request({url:"/miniprogram/user/getBrandDetail",data:{id:this.data.brand_id,user_id:this.data.user.id}}).then(res=>{
            this.setData({brand:res.data})
        })
    },
    toggleFans(){
        if(this.data.brand.is_fans == 1){
            wx.showModal({
                title:'是否取消关注？',
                success:re=>{
                    if(re.cancel) return false
                    request({url:"/miniprogram/user/toggleFans",data:{user_id:this.data.user.id,brand_id:this.data.brand_id}}).then(res=>{
                        if(res.code == 200){
                            var brand = this.data.brand
                            console.log(brand.is_fans)
                            if(brand.is_fans == 1){
                                brand.is_fans = 0
                            }else{
                                brand.is_fans = 1
                            }
                            console.log(res.data)
                            if(res.data == false){
                                res.data = 0
                            }
                            brand.fans = res.data
                            this.setData({brand})
                        }
                    })
                }
            })
        }else{
            request({url:"/miniprogram/user/toggleFans",data:{user_id:this.data.user.id,brand_id:this.data.brand_id}}).then(res=>{
                if(res.code == 200){
                    var brand = this.data.brand
                    console.log(brand.is_fans)
                    if(brand.is_fans == 1){
                        brand.is_fans = 0
                    }else{
                        brand.is_fans = 1
                    }
                    brand.fans = res.data
                    this.setData({brand})
                }
            })
        }

    },
    setBrandViewAdd(){
        request({url:"/miniprogram/user/setBrandViewAdd",data:{brand_id:this.data.brand_id}}).then(res=>{})
    },
    toHome(){
        wx.switchTab({url:'/pages/index/index'})
    },
    onShareAppMessage(res){
        return {
            title:this.data.brand.shop_name
        }
    },
})